package com.shanks.home.module.io;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.RandomAccessFile;

import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Environment;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.shanks.home.R;

public class IOActivity extends Activity {

	private Button writeBtn, readBtn, fileWriteBtn, fileReadBtn, sdWriteBtn, sdReadBtn;
	private TextView text;
	private Editor editor;
	private SharedPreferences sharedPreferences;
	private GestureDetector gestureDetector;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.activity_io);

		initView();
		initSharedPreference();
		initGesture();
	}

	private void initView() {
		writeBtn = (Button)this.findViewById(R.id.io_write_btn);
		readBtn = (Button)this.findViewById(R.id.io_read_btn);
		fileWriteBtn = (Button)this.findViewById(R.id.io_file_write_btn);
		fileReadBtn = (Button)this.findViewById(R.id.io_file_read_btn);
		sdWriteBtn = (Button)this.findViewById(R.id.io_sd_write_btn);
		sdReadBtn = (Button)this.findViewById(R.id.io_sd_read_btn);

		text = (TextView)this.findViewById(R.id.io_tv);

		writeBtn.setOnClickListener(onClickListener);
		readBtn.setOnClickListener(onClickListener);
		fileWriteBtn.setOnClickListener(onClickListener);
		fileReadBtn.setOnClickListener(onClickListener);
		sdWriteBtn.setOnClickListener(onClickListener);
		sdReadBtn.setOnClickListener(onClickListener);
	}

	private OnClickListener onClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			int id = v.getId();
			if (id == R.id.io_write_btn) {
				write();
			} else if (id == R.id.io_read_btn) {
				read();
			} else if (id == R.id.io_file_write_btn) {
				fileWrite();
			} else if (id == R.id.io_file_read_btn) {
				fileRead();
			} else if (id == R.id.io_sd_write_btn) {
				sdWrite();
			} else if (id == R.id.io_sd_read_btn) {
				sdRead();
			}
		}
	};

	private void initSharedPreference() {
		sharedPreferences = this.getSharedPreferences("home", MODE_PRIVATE);
		editor = sharedPreferences.edit();
	}

	private void write() {
		// 存入数据
		editor.putString("name", "shanks");
		editor.putInt("number", 25);
		text.setText("write:" + "name,shanks;number,25");
		editor.commit();
	}

	private void read() {
		// 读取数据
		String name = sharedPreferences.getString("name", "");
		int number = sharedPreferences.getInt("number", 0);
		text.setText("read:" + "name," + name + ";number," + number);
	}
	
	private void fileWrite() {
		try {
			// 以追加模式打开文件输出流
			FileOutputStream fos = this.openFileOutput("home.bin", MODE_APPEND);
			// 将FileOutputStream包装成PrintStream
			PrintStream ps = new PrintStream(fos);
			ps.println("shanks");
			text.setText("file write:shanks");
		} catch (Exception e) {

		}

	}

	private void fileRead() {
		try {
			// 打开文件输入流
			FileInputStream fis = this.openFileInput("home.bin");
			// 每次读取1024Byte(1KB)的数据
			byte[] buffer = new byte[1024];
			int hasRead = 0;
			StringBuffer sb = new StringBuffer("");
			while ((hasRead = fis.read(buffer)) > 0 ) {
				sb.append(new String(buffer, 0, hasRead));
			}
			text.setText("file read:" + sb);
		} catch (Exception e) {

		}
	}

	private void sdWrite() {
		try {
			// 如果手机插入了SD卡，而且应用程序具有访问SD的权限
			if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
				// 获取SD卡的目录
				File sdCardDir = Environment.getExternalStorageDirectory();
				File targetFile = new File(sdCardDir + "/home.bin");
				// 以指定文件创建RandomAccessFile对象
				RandomAccessFile rad = new RandomAccessFile(targetFile, "rw");
				// 将文件指针移动到最后
				rad.seek(rad.length());
				// 输出内容
				rad.writeChars("shanks_sd");
				text.setText("sd_write:sd_shanks");
				rad.close();
			}
		} catch (Exception e) {

		}
	}

	private void sdRead() {
		try {
			if (Environment.getExternalStorageState().equals(
					Environment.MEDIA_MOUNTED)) {
				File sdCardDir = Environment.getExternalStorageDirectory();
				// 获取指定文件对应的输入流
				FileInputStream fis = new FileInputStream(
						sdCardDir.getCanonicalPath() + "/home.bin");
				// 将指定输入流包装成BufferReader
				BufferedReader br = new BufferedReader(new InputStreamReader(fis));
				StringBuffer sb = new StringBuffer("");
				String line;
				while ((line = br.readLine()) != null) {
					sb.append(line);
				}
				br.close();
				text.setText("sd_read:" + sb);
			}
		} catch (Exception e) {
			
		}
	}

	private void initGesture() {
		gestureDetector = new GestureDetector(this, onGestureListener);
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return gestureDetector.onTouchEvent(event);
	}
	
	private OnGestureListener onGestureListener = new OnGestureListener() {
		
		@Override
		public boolean onSingleTapUp(MotionEvent e) {
			text.setText("gesture:onSingleTapUp");
			return false;
		}
		
		@Override
		public void onShowPress(MotionEvent e) {
			text.setText("gesture:onShowPress");
		}
		
		@Override
		public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
				float distanceY) {
			text.setText("gesture:onScroll");
			return false;
		}
		
		@Override
		public void onLongPress(MotionEvent e) {
			text.setText("gesture:onLongPress");
		}
		
		@Override
		public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
				float velocityY) {
			text.setText("gesture:onFling");
			return false;
		}
		
		@Override
		public boolean onDown(MotionEvent e) {
			text.setText("gesture:onDown");
			return false;
		}
	};
}